<?php
class Functions
{
    public static function daddslashes ($string, $force = 1)
    {
        if (is_array($string))
        {
            $keys = array_keys($string);
            foreach ($keys as $key)
            {
                $val = $string[$key];
                unset($string[$key]);
                $string[addslashes($key)] = self::daddslashes($val, $force);
            }
        }
        else
        {
            $string = addslashes($string);
        }
        return $string;
    }
    public static function dbQuote ($str, $noarray = false)
    {
        if (is_string($str)) return addcslashes($str, "\n\r\\'\"\032");
        if (is_int($str) or is_float($str)) return $str;
        if (is_array($str))
        {
            if ($noarray === false)
            {
                foreach ($str as &$v)
                {
                    $v = self::quote($v, true);
                }
                return $str;
            }
            else
            {
                return '';
            }
        }
        if (is_bool($str)) return $str ? '1' : '0';
        return '';
    }
    public static function dstripslashes ($string)
    {
        if (empty($string))
        {
            return $string;
        }
        if (is_array($string))
        {
            foreach ($string as $key => $val)
            {
                $string[$key] = self::dstripslashes($val);
            }
        }
        else
        {
            $string = stripslashes($string);
        }
        return $string;
    }
    /**
     * 
     * 获取全局变量 $_GET, $_POST, $_COOKIE
     * @param string $name
     * @param string $gpc
     * @param string $default
     */
    public static function gpc ($name, $gpc = '', $default = '', $forSql = false)
    {
        $return = $default;
        switch ($gpc)
        {
            case 'g':
            case 'G':
                $return = isset($_GET[$name]) ? $_GET[$name] : $default;
                break;
            case 'p':
            case 'P':
                $return = isset($_POST[$name]) ? $_POST[$name] : $default;
                break;
            case 'c':
            case 'C':
                $return = isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default;
                break;
        }
        
        if($forSql) return self::dbQuote($return);
        return $return;
    }
    
    public static function sqlgpc($name, $gpc = '', $default = '')
    {
        return self::gpc($name, $gpc, $default, true);
    }
    
    public static function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
}